import { defineStore } from 'pinia'

export const useUserStore = defineStore("user", () => {
    const userInfo = ref({})
    // 设置用户信息
    const setUserInfo = (user: any) => {
        userInfo.value = user
    }

    // 保存token
    const token = ref("")
    const setToken = (access_token: string) => {
        token.value = access_token
    }

    // 刷新token
    const setRefeshToken = (refresh_token: string) => {
        let obj = {
            refeshToken:refresh_token,
            datetime: new Date().getTime()
        }
        localStorage.setItem("refeshToken", JSON.stringify(obj))
    }

    // 退出登录
    const logOut = () => {
        userInfo.value = {}
        token.value = ""
        localStorage.clear()
    }
    return {
        userInfo,
        token,
        setUserInfo,
        setToken,
        setRefeshToken,
        logOut
    }
},
    { persist: true }
)